package com.sugar.real.estate.module.business.controller.admin.config;

import com.sugar.real.estate.framework.common.pojo.CommonResult;
import com.sugar.real.estate.framework.common.pojo.PageResult;
import com.sugar.real.estate.framework.common.util.object.BeanUtils;
import com.sugar.real.estate.module.business.controller.admin.config.vo.BizConfigPageReqVO;
import com.sugar.real.estate.module.business.controller.admin.config.vo.BizConfigRespVO;
import com.sugar.real.estate.module.business.controller.admin.config.vo.BizConfigSaveReqVO;
import com.sugar.real.estate.module.business.dal.dataobject.config.BizConfigDO;
import com.sugar.real.estate.module.business.service.config.BizConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import static com.sugar.real.estate.framework.common.pojo.CommonResult.success;
import static com.sugar.real.estate.module.business.enums.config.ConfigEnum.HOME_MIDDLE_BANNER;
import static com.sugar.real.estate.module.business.enums.config.ConfigEnum.HOME_TOP_BANNER;

@Tag(name = "管理后台 - 参数配置")
@RestController
@RequestMapping("/business/biz-config")
@Validated
public class BizConfigController {

    @Resource
    private BizConfigService bizConfigService;

    @PutMapping("/update")
    @Operation(summary = "更新参数配置")
    @PreAuthorize("@ss.hasPermission('business:biz-config:update')")
    public CommonResult<Boolean> updateBizConfig(@Valid @RequestBody BizConfigSaveReqVO updateReqVO) {
        bizConfigService.updateBizConfig(updateReqVO);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得参数配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:biz-config:query')")
    public CommonResult<BizConfigRespVO> getBizConfig(@RequestParam("id") Long id) {
        BizConfigDO bizConfig = bizConfigService.getBizConfig(id);
        return success(BeanUtils.toBean(bizConfig, BizConfigRespVO.class, o -> {
            if (HOME_TOP_BANNER.getValue().equals(o.getConfigKey()) || HOME_MIDDLE_BANNER.getValue().equals(o.getConfigKey())) {
                o.setValue(String.valueOf(o.getValue()).split(","));
            }
        }));
    }

    @GetMapping("/page")
    @Operation(summary = "获得参数配置分页")
    @PreAuthorize("@ss.hasPermission('business:biz-config:query')")
    public CommonResult<PageResult<BizConfigRespVO>> getBizConfigPage(@Valid BizConfigPageReqVO pageReqVO) {
        PageResult<BizConfigDO> pageResult = bizConfigService.getBizConfigPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BizConfigRespVO.class, o -> {
            if (HOME_TOP_BANNER.getValue().equals(o.getConfigKey()) || HOME_MIDDLE_BANNER.getValue().equals(o.getConfigKey())) {
                o.setValue(String.valueOf(o.getValue()).split(","));
            }
        }));
    }
}